jQuery(function(event) {

	$('#locationsList').find('tr').each(function(){
		var trtds = $(this).find('td');
		var sysCountryCode = trtds.eq(6).text();
		if(sysCountryCode.length > 0)
		{
			trtds.eq(9).children('a').attr('href', jQuery('#sysCitiesURL').val() + sysCountryCode);
			trtds.eq(3).children('a').attr('href', jQuery('#vendorCitiesURL').val() + sysCountryCode);
		}
		else
		{
			trtds.eq(3).children('a').hide();
			trtds.eq(9).children('a').hide();
		}
	});
	
	$('#locationsList input[type="button"].butChange').click(function(e){
		var butChange = $(this);
		butChange.hide();
		var tdButtonChange = butChange.parent();
		var tds = tdButtonChange.parent().children();
		var butNew = tds.eq(8).children('.butNew');
		butNew.attr('disabled', 'disabled');
		var vendorCountryName = tds.eq(2).text();
		var tdSysCountryTranslation = tds.eq(4);
		var oldSysCountryTranslation = tdSysCountryTranslation.text();
		var tdSysCountryName = tds.eq(5);
		var tdSysCountryCode = tds.eq(6);
		var tdButChange = tds.eq(7);
		var translationLanguage = jQuery('#s-translation-lang').val();
		tdSysCountryTranslation.text('');
		var selectMapSysCountries = $('select.mapSysCountries').clone().removeClass().appendTo(tdSysCountryTranslation).show();
		selectMapSysCountries.val(tdSysCountryCode.text().toLowerCase());

		var funCloseChangeMapping = function(e){
			funEnableControls();
			tdButtonChange.children().not('.butChange').remove();
			butChange.show();
			butNew.removeAttr('disabled');
		};

		var funEnableControls = function(e){
			tdSysCountryTranslation.find('input,select').removeAttr('disabled');
			tdButChange.find('input,select').removeAttr('disabled');
		};
		
		$('<input class="button-ok" type="button" value="' + 'BTN_OK'.translate() + '" />').click(function(e){
			var newSysCountryCode = selectMapSysCountries.val();
			if(newSysCountryCode.length == 0)
			{
				return;
			}
			tdSysCountryTranslation.find('input,select').attr('disabled', 'disabled');
			tdButChange.find('input,select').attr('disabled', 'disabled');
			$.ajax({
				url: '/a_admin/location/translation.setVendorCountryMapping',
				method: 'post',
				timeout: 20000,
				dataType: 'json',
				async: false,
				data: {
					vendorCountryName: vendorCountryName,
					systemCountryCode: newSysCountryCode
				},
				success: function(data){
					if(data.status != undefined && data.status == 'success')
					{
						data = data.data;
						tdSysCountryTranslation.text(data.newSysCountryTranslation);
						tdSysCountryName.text(data.newSysCountryName);
						tdSysCountryCode.text(data.newSysCountryCode);
						var vendorCountryNameLowerCase = vendorCountryName.toLowerCase();
						$('#locationsList').find('tr').each(function(){
							var trtds = $(this).find('td');
							if(trtds.eq(2).text().toLowerCase() == vendorCountryNameLowerCase)
							{
								trtds.eq(4).removeAttr('style').text(data.newSysCountryTranslation);
								trtds.eq(5).removeAttr('style').text(data.newSysCountryName);
								trtds.eq(6).removeAttr('style').text(data.newSysCountryCode.toUpperCase());
								trtds.eq(3).children('a').attr('href', jQuery('#vendorCitiesURL').val() + data.newSysCountryCode).show();
								trtds.eq(9).children('a').attr('href', jQuery('#sysCitiesURL').val() + data.newSysCountryCode).show();
							} 
						});
						funCloseChangeMapping();
					}
					else
					{
						alert('MAPPING_SAVING_ERROR'.translate());
						funEnableControls();
					}							
				},
				error: function(){
					alert('MAPPING_SAVING_ERROR'.translate());
					funEnableControls();
				}
			});
		}).appendTo(tdButtonChange);
		
		$('<input class="button-cancel" type="button" value="' + 'BTN_CANCEL'.translate() + '" />').click(function(){
			tdSysCountryTranslation.text(oldSysCountryTranslation);
			funCloseChangeMapping();
		}).appendTo(tdButtonChange);
		
	});

	$('#locationsList input[type="button"].butNew').click(function(e){
		var butNew = $(this);
		butNew.hide();
		var tdButtonNew = butNew.parent();
		var tds = tdButtonNew.parent().children();
		var tdButtonChange = tds.eq(7);
		var butChange = tdButtonChange.children('.butChange');
		butChange.hide();
		var vendorCountryName = tds.eq(2).text();
		var tdSysCountryTranslation = tds.eq(4);
		var oldSysCountryTranslation = tdSysCountryTranslation.text();
		var tdSysCountryName = tds.eq(5);
		var oldSysCountryName = tdSysCountryName.text();
		var tdSysCountryCode = tds.eq(6);
		var oldSysCountryCode = tdSysCountryCode.text();
		var translationLanguage = jQuery('#s-translation-lang').val();
		var selectContinents = $('select.continents').clone().removeClass().appendTo(tdButtonChange).show();

		var funCloseNewMapping = function(e){
			funEnableControls();
			tdButtonNew.children().not('.butNew').remove();
			tdButtonChange.children().not('.butChange').remove();
			tdSysCountryName.children('input').remove();
			butNew.show();
			butChange.show();
		};

		var funEnableControls = function(e){
			tds.find('input,select').removeAttr('disabled');
		};
		
		tdSysCountryTranslation.text('');
		var inputNewSysCountryTranslation = $('<input type="text" />')
			.appendTo(tdSysCountryTranslation)
		;
		
		tdSysCountryName.text('');
		var inputNewSysCountryName = $('<input type="text" />')
			.val(vendorCountryName)
			.appendTo(tdSysCountryName)
		;
		
		tdSysCountryCode.text('');
		var inputNewSysCountryCode = $('<input type="text" />')
			.appendTo(tdSysCountryCode)
		;

		tds.find('input[type="text"]').css('border', '2px solid red');
		
		$('<input class="button-ok" type="button" value="' + 'BTN_OK'.translate() + '" />').click(function(e){
			if(selectContinents.val() == '' || inputNewSysCountryName.val().replace(/[\s\r\n\t]+/, '').length == 0 || inputNewSysCountryCode.val().replace(/[\s\r\n\t]+/, '').length == 0 || inputNewSysCountryTranslation.val().replace(/[\s\r\n\t]+/, '').length == 0)
			{
				if(selectContinents.val() == '') {
					alert('CHOOSE_CONTINENT'.translate());
				}
				return;
			}
			tds.find('input,select').attr('disabled', 'disabled');
			$.ajax({
				url: '/a_admin/location/translation.newVendorCountryMapping',
				method: 'post',
				timeout: 20000,
				dataType: 'json',
				async: false,
				data: {
					vendorCountryName: vendorCountryName,
					systemCountryTranslation: inputNewSysCountryTranslation.val(),
					systemCountryName: inputNewSysCountryName.val(),
					systemCountryCode: inputNewSysCountryCode.val(),
					systemCountryContinent: selectContinents.val()
				},
				success: function(data){
					if(data.status != undefined && data.status == 'success')
					{
						data = data.data;
						tdSysCountryName.text(data.newSysCountryName);
						tdSysCountryTranslation.text(data.newSysCountryTranslation);
						tdSysCountryCode.text(data.newSysCountryCode);
						var vendorCountryNameLowerCase = vendorCountryName.toLowerCase();
						$('#locationsList').find('tr').each(function(){
							var trtds = $(this).find('td');
							if(trtds.eq(2).text().toLowerCase() == vendorCountryNameLowerCase)
							{
								trtds.eq(4).removeAttr('style').text(data.newSysCountryTranslation);
								trtds.eq(5).removeAttr('style').text(data.newSysCountryName);
								trtds.eq(6).removeAttr('style').text(data.newSysCountryCode.toUpperCase());
								trtds.eq(3).children('a').attr('href', jQuery('#vendorCitiesURL').val() + data.newSysCountryCode).show();
								trtds.eq(9).children('a').attr('href', jQuery('#sysCitiesURL').val() + data.newSysCountryCode).show();
							} 
						});
						jQuery('<option>').val(data.newSysCountryCode).text(data.newSysCountryTranslation + ' [' + data.newSysCountryName + ']').insertAfter('select.mapSysCountries option:first');
						funCloseNewMapping();
					}
					else
					{
						alert('MAPPING_SAVING_ERROR'.translate());
						funEnableControls();
					}							
				},
				error: function(){
					alert('MAPPING_SAVING_ERROR'.translate());
					funEnableControls();
				}
			});
		}).appendTo(tdButtonNew);
		
		$('<input class="button-cancel" type="button" value="' + 'BTN_CANCEL'.translate() + '" />').click(function(){
			tdSysCountryTranslation.text(oldSysCountryTranslation);
			tdSysCountryName.text(oldSysCountryName);
			tdSysCountryCode.text(oldSysCountryCode);
			funCloseNewMapping();
		}).appendTo(tdButtonNew);
		
	});

	$('#locationsList input[type="checkbox"].butVisibility').change(function(e){
		var butVisibility = $(this);
		var vendorCountryCode = butVisibility.parent().parent().children().eq(1).text();
		
		var funRevertVisibility = function(e){
 			if(e.attr('checked'))
			{
				e.removeAttr('checked');
				e.next('a').find('img').removeClass('checked');
			}
			else
			{
				e.next('a').find('img').addClass('checked');
				e.attr('checked', 'checked');
			}
		};
		
		$.ajax({
			url: '/a_admin/location/translation.changeVendorCountryVisibility',
			method: 'post',
			timeout: 20000,
			dataType: 'json',
			async: false,
			data: {
				vendorCountryCode: vendorCountryCode,
				vendorId: butVisibility.val(),
				visible: (butVisibility.attr('checked') ? 'Y' : 'N')
			},
			success: function(data){
				if(data.status != undefined)
				{
					if(data.status == 'success')
					{
						
					}
					else
					{
						alert('VISIBILITY_CHANGING_ERROR'.translate() + ': ' + data.message);
						funRevertVisibility(butVisibility);
					}
				}
				else
				{
					alert('VISIBILITY_CHANGING_ERROR'.translate());
					funRevertVisibility(butVisibility);
				}							
			},
			error: function(){
				alert('VISIBILITY_CHANGING_ERROR'.translate());
				funRevertVisibility(butVisibility);
			}
		});
	});
});
